package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverter;
import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName:WebMvcConfiguration
 * Package: com.atguigu.lease.web.admin.custom.config
 * Description:
 *
 * @Author Makabaka
 * @Create 2024/9/20 18:20
 * @Version 1.0
 */
//作用于mvc容器上  缺点是对每一个枚举类都需要写一次转换器  使用工厂模式简化
@Configuration
//注释掉是因为使用了转换器工厂来进行类型转换  不用mvc提供的转换器
public class WebMvcConfiguration implements WebMvcConfigurer {

//    mvc提供的转换器
//    @Autowired
//    private StringToBaseEnumConverter stringToBaseEnumConverter;

//    工厂模式进行类型转换
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Value("${admin.auth.path-patterns.include}")
    private String[] includePathPatterns;

    @Value("${admin.auth.path-patterns.exclude}")
    private String[] excludePathPatterns;



//    添加拦截器  使自定义的拦截器生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.authenticationInterceptor).addPathPatterns(includePathPatterns).excludePathPatterns(excludePathPatterns);
    }
//    普通转换器
//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        registry.addConverter(this.stringToBaseEnumConverter);
//    }

//    工厂模式转换
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }
}
